package thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SyncDemo4 {
    public static void main(String[] args) {
//        List<Integer> list1 = new ArrayList<>();
        List<Integer> list1 = Collections.synchronizedList(new ArrayList<>());
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for(int i=1;i<=1000;i++){
                    list1.add(1);
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i =1;i<=1000;i++) {
                    list1.add(2);
                }
            }
        };
        t1.start();
        t2.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(list1.size());
    }
}
